package controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import model.*;
import daos.*;


public class AssignCoursesController {

	private ApplicationContext applicationContext;

	public void assignCourses(ArrayList<Integer> subjectIds, int studentId) 
	{

		applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath:persistenceTest/applicationContext.xml"});
		
		StudentProfileDAO	sProfileDAO = (StudentProfileDAO)applicationContext.getBean("studentProfileDAO");
		
		CourseDAO	cDAO = (CourseDAO)applicationContext.getBean("courseDAO");
		
		List<Course> csList= new ArrayList<Course>();
		
		for(int i=0; i< subjectIds.size() ; i++)
		{
			Course newCourse= cDAO.findById(subjectIds.get(i));
			csList.add(newCourse);						
		}
				
		StudentProfile student = sProfileDAO.findStudentById(studentId);
		 
		student.setDesiredCourses(csList);
		
		sProfileDAO.save(student);
	}
}

